#include<unistd.h>
#include<time.h>
#include"BlockQueue.hpp"
#include"Task.hpp"
static int defaultnum=20;
class ThreadData
{
public:
    string threadname_;
    BlockQueue<Task>*bq_;
};
void *Consumer(void *args)
{
    ThreadData*thread=static_cast<ThreadData*>(args);
    BlockQueue<Task>* bq=thread->bq_;
    string name=thread->threadname_;
    while(true){
        Task number=bq->pop();
        number();
        cout <<name<< ":处理了一个数据结果是" <<number.getresult()<<"  "<< endl;
    }

}
void *Productor(void *args)
{
   ThreadData*thread=static_cast<ThreadData*>(args);
    BlockQueue<Task>* bq=thread->bq_;
    string name=thread->threadname_;
    int len=opers.size();
    while(true)
    {
        int x=rand()%10;
        usleep(5);
        int y=rand()%10;
        int o=rand()%len;
        char op=opers[o];
        Task n(x,y,op);
        bq->push(n,name);
        sleep(1);
    }
}
int main()
{
    BlockQueue<Task>*rq=new BlockQueue<Task>(20);
    srand(time(nullptr));   
    pthread_t c[5],p[3];
  
    for(int i=0;i<3;i++){  
        ThreadData* t=new ThreadData;
        t->bq_=rq;
        t->threadname_="Productor-"+to_string(i);
        pthread_create(p+i,nullptr,Productor,t);
    }
    for(int i=0;i<5;i++){
        ThreadData* t=new ThreadData;
        t->bq_=rq;
        t->threadname_="Consumer-"+to_string(i);
        pthread_create(c+i,nullptr,Consumer,t);
    }
    for(int i=0;i<5;i++)
        pthread_join(c[i], nullptr);
    for(int i=0;i<3;i++)  
        pthread_join(p[i],nullptr);    
    return 0;
}